package com.wdzl.dao.impl;

import com.wdzl.dao.IpercentDao;
import com.wdzl.pojo.Percent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PercentDao implements IpercentDao {
    private final JdbcTemplate jdbcTemplate;

    public PercentDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void insert(Percent percent) {
String sql="insert into percent(name,age,sex,salary,birthday,introduce) values(?,?,?,?,?,?)";
jdbcTemplate.update (sql,percent.getName (),percent.getAge (),percent.getSex (),percent.getSalary (),percent.getBirthday (),percent.getIntroduce ());
    }

    @Override
    public void delete(Integer id) {
String sql="delete from percent where id=?";
jdbcTemplate.update (sql,id);
    }

    @Override
    public void update(Percent percent) {
String sql="update percent set image=? where id=?";
jdbcTemplate.update (sql,percent.getImage (),percent.getId ());
    }

    @Override
    public Percent selectone(Integer id) {
        String sql="select * from percent where id=?";
        Percent percent = jdbcTemplate.queryForObject (sql, new BeanPropertyRowMapper<> (Percent.class), id);

        return percent;
    }

    @Override
    public List<Percent> select() {
        String sql="select * from percent";
        List<Percent> query = jdbcTemplate.query (sql, new BeanPropertyRowMapper<> (Percent.class));
        return query;
    }

    @Override
    public void up(Percent percent) {
        String sql="update percent set name=?,age=?,sex=?,birthday=?,introduce=? where id=?";
        jdbcTemplate.update (sql,percent.getName (),percent.getAge (),percent.getSex (),percent.getBirthday (),percent.getIntroduce (),percent.getId ());
    }

    @Override
    public int zhuan(String sid, float money)  {
        String sql="update percent set salary=salary-? where id=?";
        try {
            int zhuan = jdbcTemplate.update (sql, money, sid);
            return zhuan;
        }catch (Exception e){
            throw new RuntimeException ("xxxxxx");
        }

    }

    @Override
    public int shou(String aid, float money) {
        String sql="update percent set salary=salary+? where id=?";
        try {
        int shou = jdbcTemplate.update (sql, money, aid);
        return shou;
        }catch (Exception e){
            throw new RuntimeException ("xxxxxx");
        }
    }

}
